﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

namespace ConsoleApplication1
{
    #region 测试1
    class test1
    {
        static int i = GetNum();
        int j = GetNum();
        static int number = 1;


        static int GetNum()
        {
            return number;
        }

        static void Main()
        {
            Console.WriteLine("i={0}", i);
            Console.WriteLine("j={0}", new test1().j);
            //double i = Math.IEEERemainder(5.0, 3.0);
            //Console.WriteLine(i);
            Console.Read();

        }
    }

    class test2
    {
        private string str = "Class.str";
        private int i = 0;
        static void StringConvert(string str)
        {
            str = "string being Converted";
        }

        static void StringConvert(test2 t)
        {
            t.str = "string being Converted";
        }

        static void Add(int i)
        {
            i++;
        }

        static void AddWithRef(ref int i)
        {
            i++;
        }

        static void Main()
        {
            int i1 = 10;
            int i2 = 20;
            string str = "str";
            test2 t = new test2();
            Add(i1);
            AddWithRef(ref i2);
            Add(t.i);
            StringConvert(str);
            StringConvert(t);
            Console.WriteLine(i1);
            Console.WriteLine(i2);
            Console.WriteLine(t.i);
            Console.WriteLine(str);
            Console.WriteLine(t.str);
            Console.Read();
        }
    }
    #endregion

    #region 测试2
    public abstract class test3
    {
        public test3()
        {
            Console.WriteLine("test3");
        }

        public virtual void Func()
        {
            Console.WriteLine("test3.Func()");
        }
    }

    public class test4 : test3
    {
        public test4()
        {
            Console.WriteLine("test4");
        }

        public new void Func()
        {
            Console.WriteLine("test4.Func()");
        }

        static void Main()
        {
            test3 a = new test4();
            a.Func();
            Console.Read();
        }
    }
    #endregion

    #region 测试3
    public class test5
    {
        public virtual void Fun1(int i)
        {
            Console.WriteLine(i);
        }

        public void Fun2(test5 a)
        {
            a.Fun1(1);
            Fun1(5);
        }
    }

    public class test6 : test5
    {
        public override void Fun1(int i)
        {
            base.Fun1(i + 1);
        }

        public static void Main()
        {
            test6 b = new test6();
            test5 a = new test5();
            a.Fun2(b);
            b.Fun2(a);
            Console.Read();
        }
    }
    #endregion

    #region 测试4
    class A
    {
        public A()
        {
            PrintFields();
        }

        public virtual void PrintFields() { }
    }

    class B : A
    {
        int x = 1;
        int y;
        public B()
        {
            y = -1;
            PrintFields();
        }

        public override void PrintFields()
        {

            Console.WriteLine("x={0},y={1}", x, y);

        }

        static void Main()
        {
            new B();
            //short s1 = 1;
            //s1 = s1 + ((short)1);
            //s1 += 1;
            //Console.WriteLine(s1);
            Console.Read();
        }
    }
    #endregion

    #region 测试5
    class Animal
    {
      public virtual string DoSomething()
      {
        return "I can do something";
      }

      static void Main()
      {
          Animal a = new Animal();
          Console.WriteLine(a.DoSomething());
          //Bird b = new Bird();
          Animal b = new Bird();
          Console.WriteLine(b.DoSomething());
          //Cat c = new Cat();
          Animal c = new Cat();
          Console.WriteLine(c.DoSomething());

          Console.Read();
      }
    }

    class Bird : Animal
    {
        public override string DoSomething()
        {
            return "I can fly";
        }
    }

    class Cat : Animal
    {
        public new string DoSomething()
        {
            return "I can run";
        }
    }
    #endregion

    #region 测试6--泛型
    class test7<T> where T : class
    { }

    class MyClass<K, V>
    {
        public V this[K key]
        {
            get
            {
                return this[key] ;
               //throw new NotImplementedException();
            }
        }
    }

    class MyClass
    {
        public static T[] MyMethod1<T>(T arg)
        {
            throw new NotImplementedException();
        }

        public static K MyMethod2<K, V>(V value, MyClass<K, V> dictionary)
        {
            throw new NotImplementedException();
        }
    }

    class test8
    {
        static void Main()
        {
            //test7<int> t = new test7<int>(); 错误代码
            //test7<string> t2 = new test7<string>();

            //int[] result = SomeMethod(1, 20);

            MyClass<int, string> instance = new MyClass<int, string>();
            Object result = MyClass.MyMethod1(MyClass.MyMethod2(instance[0], instance));
        }

        public static T[] SomeMethod<T>(T value,int size)
        {
            T[] arr = new T[size];
            for (int i = 0; i < size; i++)
            {
                arr[i] = value;
            }
            
            return arr;
        }
    }
    #endregion

    #region 随机读取数组
    class test9
    {
        static void Main()
        {
            StringBuilder sb = new StringBuilder(0, 10000);
            string strABC = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
            string[] ABC = strABC.Split(',');
            int len = ABC.Length;
            Random rd = new Random();
            for (int i = 0; i < 10000; i++)
            {
                sb.Append(ABC[rd.Next(len)]);
            }
            Console.Write(sb.ToString());
            Console.Read();
        }


    }
    #endregion

    #region 单例模式
    //饿汉模式，提前初始化，线程安全
    class test10
    {
        private test10() { }
        private static test10 t = new test10();

        public static test10 GetInstance()
        {
            Console.WriteLine("被实例化");
            return t;
        }
    }

    //懒汉模式，延迟加载
    class test12
    {
        private test12(){}
        private static test12 t= null;
        private static readonly object obj = new object();
        public static test12 GetInstance()
        {
            if (t == null)
            {
                lock (obj)
                {
                    if (t == null)
                    {
                        t = new test12();
                    }
                }
            }
            return t;
        }
    }

    class test11
    {
        static void Main()
        {
            //单例
            test10 t1 = test10.GetInstance();
            test10 t2 = test10.GetInstance();
            Console.WriteLine(t1.Equals(t2));

            //非单例
            test1 t3 = new test1();
            test1 t4 = new test1();
            Console.WriteLine(t3.Equals(t4));
            Console.WriteLine(t3 == t4);
            Console.Read();
        }
    }
    #endregion

    #region 扩展方法
    public class User
    { }

    public static class UserExtension 
    { 
        public static bool IsNull(this User user) 
        { return null == user; } 
    }

    public class test13
    {
        static void Main()
        {
            User u = null;//= new User();
            Console.WriteLine(u.IsNull());
            Console.Read();
        }
    }
    #endregion
    
    #region abstract,virtical,override
    abstract class AbstractClass
    {
        public void RealMethod()
        {
            Console.WriteLine(@"The ""RealMethod"" method of ""AbstractClass"" class！");
        }

        public virtual void ViutualMethod()
        {
            Console.WriteLine(@"The ""ViutualMethod"" method of ""AbstractClass""class！");
        }

        public virtual string Name
        {
            get
            {
                return "";
            }
        }
    }

    class BaseClass
    {
        public void RealMethod()
        {
            Console.WriteLine(@"The ""RealMethod"" method of ""BaseClass"" class！");
        }

        public virtual void ViutualMethod()
        {
            Console.WriteLine(@"The ""ViutualMethod"" method of ""BaseClass""class！");
        }
    }

    class NewAndOverride : BaseClass  //AbstractClass
    {
        public new void RealMethod()
        {
            Console.WriteLine(@"The ""RealMethod"" method of ""NewAndOverride"" class！");
        }

        public override void ViutualMethod()
        {
            Console.WriteLine(@"The ""ViutualMethod"" method of ""NewAndOverride""class！");
        }

        //public override void AbstractMethod()
        //{
        //    Console.WriteLine(@"The ""AbstractMethod"" method of ""NewAndOverride""class！");
        //}
    }

    class test14
    {
        public static void Main()
        {
            BaseClass b2 = new BaseClass();
            b2.ViutualMethod();

            BaseClass b1 = new NewAndOverride();
            //b1.ViutualMethod();
            //b1.RealMethod();
            NewAndOverride Nb = new NewAndOverride();
            //Nb.RealMethod();
            //Nb.AbstractMethod();

            Console.Read();
        }
    }
    #endregion
}
